﻿<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px; margin:0px; text-align:center}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

date_default_timezone_set("Asia/Shanghai");

@ $matchId = $_GET["matchId"];
@ $type = $_GET["type"];
@ $date = $_GET["date"];

if ($matchId == "") {
	return;
}
if ($date == "") {
	$date = date("ym");
} else {
	$date = explode("-", $date);
	$year = substr($date[0], -2);
	$date = $year.$date[1];
}


$team = NULL;
$first = NULL;
$id = NULL;
$name = NULL;
$field = NULL;
$free = NULL;
$three = NULL;
$off = NULL;
$def = NULL;
$o = NULL;
$ass = NULL;
$fouls = NULL;
$ste = NULL;
$turn = NULL;
$blo = NULL;
$points = NULL;

//先清空当前比赛场次的数据
$sql = "delete from live_member_stats_b where matchId = '" . $matchId . "';";
$sql .= "INSERT INTO live_member_stats_b (matchId, team, first, id, name, field, three, free, off, def, o, ass, ste, blo, turn, fouls, points) VALUES";

if ($type == "NBA") {
	$url = "http://sports.sinajs.cn/" . $date . "/score_" . $matchId . ".js?" . rand();

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
	$matchStats = curl_exec($ch);
	curl_close($ch);
	if($matchStats == ""){
		//切换到高速模式
		$url = "http://sports.sina.com.cn/nba/iframe/js/" . $date . "/score_" . $matchId . ".js?" . rand();
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
		$matchStats = curl_exec($ch);
		curl_close($ch);
	}
	$matchStats = iconv("GBK", "UTF-8", $matchStats);
	$matchStats = str_replace("basket_scores_render_stats(", "", $matchStats);
	$matchStats = str_replace(");", "", $matchStats);

	$matchStats = str_replace(",,", "", $matchStats);
	$matchStats = str_replace("'", "", $matchStats);
	$matchStats = str_replace(" ", "", $matchStats);
	$matchStats = str_replace("\t", "", $matchStats);
	$matchStats = str_replace("\r\n", "", $matchStats);

	$matchStats = str_replace(",", "\",\"", $matchStats);
	$matchStats = str_replace(":", "\":\"", $matchStats);

	$matchStats = str_replace("\"{", "{", $matchStats);
	$matchStats = str_replace("{", "{\"", $matchStats);

	$matchStats = str_replace("}", "\"}", $matchStats);
	$matchStats = str_replace("}\"", "}", $matchStats);

	$matchStats = str_replace("\"[", "[", $matchStats);
	//$matchStats = str_replace("[", "[\"", $matchStats);

	$matchStats = str_replace("]\"", "]", $matchStats);
	//$matchStats = str_replace("]", "\"]", $matchStats);

	$matchStats = json_decode($matchStats);

	//主队首发
	$homestart = $matchStats->homestart;
	foreach ($homestart as $home) {
		$sql .= "('" . $matchId . "', '1', 'Y', '" . $home->id . "', '" . $home->n . "', '" . $home->field . "', '" . $home->three . "', '" . $home->free . "', '" . $home->off . "', '" . $home->def . "', '" . $home->o . "', '" . $home->ass . "', '" . $home->ste . "', '" . $home->blo . "', '" . $home->turn . "', '" . $home->fouls . "', '" . $home->points . "'),";
	}
	//主队替补
	$homeplayer = $matchStats->homeplayer;
	foreach ($homeplayer as $home) {
		$sql .= "('" . $matchId . "', '1', 'N', '" . $home->id . "', '" . $home->n . "', '" . $home->field . "', '" . $home->three . "', '" . $home->free . "', '" . $home->off . "', '" . $home->def . "', '" . $home->o . "', '" . $home->ass . "', '" . $home->ste . "', '" . $home->blo . "', '" . $home->turn . "', '" . $home->fouls . "', '" . $home->points . "'),";
	}
	//客队首发
	$gueststart = $matchStats->gueststart;
	foreach ($gueststart as $home) {
		$sql .= "('" . $matchId . "', '2', 'Y', '" . $home->id . "', '" . $home->n . "', '" . $home->field . "', '" . $home->three . "', '" . $home->free . "', '" . $home->off . "', '" . $home->def . "', '" . $home->o . "', '" . $home->ass . "', '" . $home->ste . "', '" . $home->blo . "', '" . $home->turn . "', '" . $home->fouls . "', '" . $home->points . "'),";
	}
	//客队替补
	$guestplayer = $matchStats->guestplayer;
	foreach ($guestplayer as $home) {
		$sql .= "('" . $matchId . "', '2', 'N', '" . $home->id . "', '" . $home->n . "', '" . $home->field . "', '" . $home->three . "', '" . $home->free . "', '" . $home->off . "', '" . $home->def . "', '" . $home->o . "', '" . $home->ass . "', '" . $home->ste . "', '" . $home->blo . "', '" . $home->turn . "', '" . $home->fouls . "', '" . $home->points . "'),";
	}
} else {
	$url = "http://sports.sinajs.cn/livecast/js/basketball_" . $matchId . ".js?" . rand();

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
	$matchStats = curl_exec($ch);

	curl_close($ch);
	$matchStats = iconv("GBK", "UTF-8", $matchStats);

	$matchStats = str_replace("\n", "", $matchStats);
	$matchStats = str_replace("var c1 = ['", "", $matchStats);
	$matchStats = str_replace("var c2 = ['", "", $matchStats);
	$matchStats = str_replace("var s1 = ['", "", $matchStats);
	$matchStats = str_replace("var s2 = ['", "", $matchStats);
	$matchStats = str_replace("']", "", $matchStats);

	$matchStats = explode(";", $matchStats);
	$s1 = $matchStats[2];
	$s2 = $matchStats[3];

	$s1 = explode("','", $s1);
	$s2 = explode("','", $s2);

	foreach ($s1 as $home) {
		$home = explode(",", $home);
		if("总计" == $home[0]){
			continue;
		}
		$first = "N";
		if ($home[3] == "是") {
			$first = "Y";
		}
		$sql .= "('" . $matchId . "', '1', '" . $first . "', '" . $home[1] . "', '" . $home[0] . "', '" . $home[4] . "', '" . $home[5] . "', '" . $home[6] . "', '" . $home[7] . "', '" . $home[8] . "', '" . ($home[7]+$home[8]) . "', '" . $home[9] . "', '" . $home[11] . "', '" . $home[13] . "', '" . $home[12] . "', '" . $home[10] . "', '" . $home[17] . "'),";
	}
	
	foreach ($s2 as $home) {
		$home = explode(",", $home);
		if("总计" == $home[0]){
			continue;
		}
		$first = "N";
		if ($home[3] == "是") {
			$first = "Y";
		}
		$sql .= "('" . $matchId . "', '2', '" . $first . "', '" . $home[1] . "', '" . $home[0] . "', '" . $home[4] . "', '" . $home[5] . "', '" . $home[6] . "', '" . $home[7] . "', '" . $home[8] . "', '" . ($home[7]+$home[8]) . "', '" . $home[9] . "', '" . $home[11] . "', '" . $home[13] . "', '" . $home[12] . "', '" . $home[10] . "', '" . $home[17] . "'),";
	}
}
$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";


//连接数据库
$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);
//插入比赛比分
mysqli_multi_query($conn, $sql);

echo "球员技术统计数据采集";
?>